/**
 * ExecuteTimeInterceptor.java
 * com.sftz.framework.aop
 * Copyright (c) 2019, 北京科技有限公司版权所有.
*/

package com.sftz.framework.aop;

import java.lang.reflect.Method;

import org.nutz.aop.InterceptorChain;
import org.nutz.aop.interceptor.AbstractMethodInterceptor;
import org.nutz.log.Log;
import org.nutz.log.Logs;

/**
 * 用于跟踪方法的执行时间
 * <p>
 *
 * @author   朱晓川
 * @Date	 2019年1月23日 	 LoggingMethodInterceptor
 *
 */
public class ExecuteTimeInterceptor extends AbstractMethodInterceptor {

	private static final Log LOG = Logs.get();
	/** 
	 * 阈值,如果执行时间超过1秒，则打印日志
	 */
	private static final long THRESHOLD = 1000;

	@Override
	public void filter(final InterceptorChain chain) throws Throwable {
		Method method = chain.getCallingMethod();

		long begin = System.currentTimeMillis();
		chain.doChain();
		long time = System.currentTimeMillis() - begin;
		if (time > THRESHOLD) {
			LOG.warnf("[slow] Method = %s , executetime = %s ms", method, time);
		}
	}
}